package com.aglhz.nature.presenter;

import java.util.ArrayList;
import java.util.List;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import android.util.Log;

import com.aglhz.nature.constants.ServerAPI;
import com.aglhz.nature.events.ClassificationViewEvent;
import com.aglhz.nature.modle.ClassScrollViewImage;
import com.aglhz.nature.modle.ClassificationImage;
import com.aglhz.nature.modle.item.classification.ClassificationData;
import com.aglhz.nature.presenter.base.BasePresenter;
import com.aglhz.nature.view.iv.ClassificationView;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.TextHttpResponseHandler;

public class ClassificationPresenter extends BasePresenter {
	private static String TAG = ClassificationPresenter.class.getName();
	// view
	private ClassificationView classificationView;

	// data
	private ClassificationImage classificationImage;
	private List<ClassificationData> listData;
	private ClassScrollViewImage classScrollViewImage;
	
	


	public ClassificationPresenter(ClassificationView view) {
		classificationView = view;
	
	}

	// /*********************data operation*******************///
	public void loadData() {
		// load remote data
		this.loadScrollImage();
		this.loadGridImage();
		// save remote data
		

	}

	// /*********************view operation*************fen lei******///
	public void showGridView() {
		// get data array list
		classificationView.setList(classificationImage.getData());
			
	}
//lu bo tu
public void showScrollView(){
	List<String>scrollImageUrls=new ArrayList<String>();
	
	if(!(0==classScrollViewImage.getData().size())){
		for (int i = 0; i < classScrollViewImage.getData().size(); i++) {
			scrollImageUrls.add(classScrollViewImage.getData().get(i).getPicture());
			
		}
		classificationView.showSrcollImage(scrollImageUrls);
	}else{
		classificationView.dismissScrollImage();
	}

}
	
//public List<String> getBrindId(){
//	// get image's URL array list
//			List<String> BrindIds = new ArrayList<String>();
//				for(int i=0;i<classScrollViewImage .getData().size(); i++){
//					BrindIds.add(classScrollViewImage .getData().get(i).getGoodsId());
//					}
//				
//	return BrindIds;
//	
//	
//}

public List<String> getLink(){
	List<String> links= new ArrayList<String>();
	for(int i=0;i<classScrollViewImage .getData().size(); i++){
		 links.add(classScrollViewImage .getData().get(i).getLink());
		
	}
	return links;
}


///********************view operation*******************///
	@Subscribe(threadMode = ThreadMode.MAIN)
	public void onEventClassificationThread(ClassificationViewEvent event) {

		String msg = event.getMessage();
		Log.i(TAG, "EventClassificationThread:" + msg);
		// update UI By event
		if (ClassificationViewEvent.EVENT_UPDATE_GRIDVIEW_IMAGE == msg) {
			// update grid view
			this.showGridView();
		} else if(ClassificationViewEvent.EVENT_UPDATE_SCROLLVIEW_IMAGE == msg){
			this.showScrollView();
		}
		

	}
	
	///*********************private method*********lun bo tu**********///
		private void loadScrollImage() {
			String url = ServerAPI.APIHEARD+ServerAPI.CLSSIFICATION_SCROLLVIEW;
			
			AsyncHttpClient client = new AsyncHttpClient();
			client.get(url, new TextHttpResponseHandler() {

				@Override
				public void onSuccess(int statusCode,
						cz.msebera.android.httpclient.Header[] headers,
						String responseString) {
					try {
						Log.e(TAG, "response" + responseString);

						Gson gson = new Gson();

						ClassScrollViewImage classBean = gson.fromJson(responseString,
								ClassScrollViewImage.class);
						classScrollViewImage = (ClassScrollViewImage) classBean.clone();

						// send event
						ClassificationViewEvent event = new ClassificationViewEvent(
								ClassificationViewEvent.EVENT_UPDATE_SCROLLVIEW_IMAGE);
						EventBus.getDefault().post(event);
					} catch (Exception e) {
						e.printStackTrace();
					}

				}

				@Override
				public void onFailure(int statusCode,
						cz.msebera.android.httpclient.Header[] headers,
						String responseString, Throwable throwable) {

				}

			});

		}
		

	// /*********************private method*******************///
	private void loadGridImage() {

		String url = ServerAPI.APIHEARD+ServerAPI.CLASSIFICATION_RECOMMEND;

		AsyncHttpClient client = new AsyncHttpClient();
		client.get(url, new TextHttpResponseHandler() {
			
			@Override
			public void onSuccess(int statusCode,
					cz.msebera.android.httpclient.Header[] headers,
					String responseString) {
				 try {
					 Log.e(TAG, "response" + responseString);
					
					 Gson gson = new Gson();
					
					ClassificationImage cBean = gson.fromJson(responseString,
					ClassificationImage.class);
					classificationImage = (ClassificationImage) cBean.clone();
					
					Log.d(TAG,"classificationImage---111-----"+classificationImage.getOther());	
					listData = classificationImage.getData();
					Log.d(TAG,"classificationImage---mcp-----"+classificationImage.getData());	
					 // send event
					 ClassificationViewEvent event = new  ClassificationViewEvent(
							 ClassificationViewEvent.EVENT_UPDATE_GRIDVIEW_IMAGE);
					 EventBus.getDefault().post(event);
					 
					 Log.e(TAG, "success" + "success");
					 } catch (Exception e) {
					 e.printStackTrace();
					 }
				
				
			}
			
			@Override
			public void onFailure(int statusCode,
					cz.msebera.android.httpclient.Header[] headers,
					String responseString, Throwable throwable) {
				
				 Log.e(TAG, "failure");
			}
		

	

	
		});

	}
		
		
		
		
		
		
		
		
	private void loadListImage() {

	}

}

